<template>
  <info-card :name="model.name" show-state show-btns>
    <template v-slot:btns>
      <el-button v-if="!editable" type="text" size="mini" icon="el-icon-edit" @click="handleEdit">编辑</el-button>
      <el-button v-if="editable" type="text" size="mini" icon="el-icon-circle-check" @click="handleSubmit">保存
      </el-button>
      <el-button v-if="editable" type="text" size="mini" icon="el-icon-circle-close" @click="handleCancel">取消
      </el-button>
    </template>
    <template v-slot:state>
      <dict-tag :options="localDict.role_status" :value="model.status"/>
    </template>
    <template v-slot:content>
      <el-form v-if="editable" ref="form" :model="formdata" :rules="formrules" label-suffix="：" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="角色类型" prop="type">
              <el-select v-model="formdata.type" placeholder="请选择角色类型" style="width: 100%">
                <el-option v-for="item in localDict.role_type" :key="item.value" :label="item.label"
                           :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="角色状态" prop="status">
              <el-select v-model="formdata.status" placeholder="请选择角色状态" style="width: 100%">
                <el-option v-for="item in localDict.role_status" :key="item.value" :label="item.label"
                           :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="描述信息" prop="remark">
          <el-input v-model="formdata.remark" placeholder="请输入描述信息" type="textarea"/>
        </el-form-item>
      </el-form>
      <el-descriptions v-else :class="['description', 'full']" :column="2" contentClassName="content"
                       labelClassName="label">
        <el-descriptions-item label="角色类型">
          <dict-tag :options="localDict.role_type" :value="model.type" show-label/>
        </el-descriptions-item>
        <el-descriptions-item label="权限范围">
          <dict-tag :options="localDict.role_scope" :value="model.scope" show-label/>
        </el-descriptions-item>
        <el-descriptions-item :span="2" label="备注说明">{{ model.remark | formatInfo }}</el-descriptions-item>
      </el-descriptions>
    </template>
  </info-card>
</template>
<script>
import {getStaticData} from "../../../../api/common";
import {getOne, update} from "../../../../api/base";

export default {
  name: 'RoleInfoCard',
  dicts: [],
  props: {id: String, model: Object},
  filters: {
    formatInfo(val) {
      return val ? val : '-'
    }
  },
  data() {
    return {
      // model: {},
      baseUrl: '/system/role/',
      editable: false,
      formdata: {},
      formrules: {},
      localDict: {
        role_type: [],
        role_scope: [],
        role_status: []
      },
    }
  },
  created() {
    this.getLocalDict()
      this.getModel()
  },
  computed: {},
  methods: {
    getLocalDict() {
      this.getStaticData('system-role-type').then(res => {
        this.localDict.role_type = res.data
      })
      this.getStaticData('system-role-scope').then(res => {
        this.localDict.role_scope = res.data
      })
      this.getStaticData('system-status').then(res => {
        this.localDict.role_status = res.data
      })
    },
    getModel() {
      // 获取表详细信息
      getOne(this.baseUrl, this.id).then(res => {
        this.model = res.data;
      });
    },
    handleEdit() {
      this.editable = true
      this.formdata = {...this.model}
    },
    handleSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          update(this.baseUrl, this.formdata).then(res => {
            this.$modal.msgSuccess("保存成功");
            this.handleCancel()
            this.$emit('reload')
            // this.getModel()
          })
        }
      });
    },
    // 取消按钮
    handleCancel() {
      this.editable = false;
      this.resetForm('form')
    },
  }
};
</script>
<style lang="scss" scoped>

</style>
